require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

var myFun = {};
require(['jquery', 'doT', 'common', 'bootstrap'], function($, doT, common) {
    $(function() {
        function getId() {
            var p = location.search.split('?')[1];
            var items = p.split('&');
            myFun.item = {};
            $.each(items, function() {
                var key = this.split('=')[0];
                var value = this.split('=')[1];
                myFun.item[key] = value;
            });
        }
        getId();

        var id = myFun.item['id']; //获取要修改的管理员的ID

        function initAdmin() {
            $.get(common.urlBase + '/account/get/' + id + '.json', function(data, statu) {
                var data = eval('(' + data + ')');
                if (data['ok']) {
                    for (var d in data.data) {
                        if (d != 'id' && d != 'gender') {
                            $('#' + d).val(data.data[d]);
                        }
                        $('#admin_name').val(data.data['name']);
                        data['gender'] == '2' ? $('#female').attr("checked", "checked") : $('#male').attr("checked", "checked");
                    }
                } else {
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                    return false;
                }
            });
        }
        initAdmin();

        $('#change_admin').submit(function() {
            var account = $('#account').val();
            var pwd = $('#password').val();
            var name = $('#admin_name').val();
            var genderId = $('input:radio:checked').attr('id');
            var gender = genderId == 'male' ? 1 : 2;
            var phone = $('#phone').val();
            var email = $('#email').val();
            var role = 1;

            var data = {
                'id': id,
                'name': name,
                'gender': gender,
                'account': account,
                'password': pwd,
                'phone': phone,
                'email': email,
                'role': role
            };
            $.post(common.urlBase + '/account/edit.json', data, function(data, statu) {
                data = eval('(' + data + ')');
                if (data['ok']) {
                    $('#noticeMsg').text('修改成功');
                    $('#noticeModal').modal('show');
                    window.location = '/views/manager.html';
                } else {
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                }
            });
            return false;
        });

        $('.sidebar ul li').each(function() {
            if ($(this).text() == '查看管理员') {
                $(this).parent().css('display', 'block');
                $(this).children().css('color', '#46A3FF');
            }
        });
    });



});
